<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta content="text/html;charset=utf-8">
    <title>医生操作界面</title>
    <meta content='width=device-width, initial-scale=1.0, shrink-to-fit=no' name='viewport'/>
    <link rel="icon" href="../../static/assets/img/icon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../../static/layui/css/layui.css">
    <%--    <link rel="stylesheet" href="${pageContext.servletContext.contextPath}/static/layui/dropMenu/dropMenu.css">--%>
    <script src="../../static/layui/layui.js"></script>
</head>
<body data-background-color="bg3">

<table class="layui-table" lay-filter="test" id="reserve" lay-data="{id:'id'}"></table>

<script src="${pageContext.servletContext.contextPath}/webjars/jquery/3.1.1/jquery.min.js"></script>

<script type="text/html" id="barDemo">
    <%--    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>--%>
    <%--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>--%>
    <%--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>--%>

    <a class="layui-btn layui-btn-xs" lay-event="takeOver" style="color: #fff">接受</a>
    <a class="layui-btn layui-btn-xs" lay-event="refuse" style="color: #fff">拒绝</a>


    <%--    <input type='button' class='layui-btn rowmoreBtn layui-icon layui-icon-more' value="更多"/>--%>

    <%--    <button class="layui-btn" id="more">--%>
    <%--        更多--%>
    <%--        <i class="layui-icon layui-icon-down layui-font-12"></i>--%>
    <%--    </button>--%>

</script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">接受</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">拒绝</button>
        <%--        <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>--%>
    </div>
</script>
<%--<script src="${pageContext.servletContext.contextPath}/static/layui/dropMenu/dropMenu.js"></script>--%>
<script>
    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1,                 //月份
            "d+": this.getDate(),                    //日
            "h+": this.getHours(),                   //小时
            "m+": this.getMinutes(),                 //分
            "s+": this.getSeconds(),                 //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds()             //毫秒
        };

        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }

        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(
                    RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }

        return fmt;
    }

    var tableLns;
    $(() => {
        layui.use('table', function () {
            let table = layui.table;

            tableLns = table.render({
                id: "reserve",
                elem: '#reserve'
                , url: '${pageContext.servletContext.contextPath}/reserve/appointmentNotProcessed'
                , toolbar: "#toolbarDemo"
                , cols: [[
                    {type: "checkbox"}
                    , {field: 'RESERVE_NUM', minWidth: 188, title: '预约挂号编号', sort: true}
                    , {field: "NICKNAME", minWidth: 106, title: "用户昵称", sort: true}
                    , {
                        edit: "text", minWidth: 163, title: '预约时间', sort: true,
                        templet: function (value) {
                            if (value.TIME_OF_APPOINTMENT == undefined) {
                                return "";
                            }
                            let date = new Date();
                            console.log(value.TIME_OF_APPOINTMENT);
                            date.setTime(value.TIME_OF_APPOINTMENT);
                            let dateFormat1 = date.format("yyyy-MM-dd hh:mm:ss");
                            console.log(dateFormat1);
                            return dateFormat1;
                        }
                    }
                    , {field: "DIAGNOSE_TYPE", title: '看诊类型', minWidth: 86, sort: true}
                    , {
                        title: '科室名称', minWidth: 86, sort: true
                        , templet: function (value) {
                            if (value.MedicalOffice != undefined && value.MedicalOffice.officeName != undefined) {
                                return value.MedicalOffice.officeName;
                            }
                            return "";
                        }
                    }
                    , {
                        title: '医生', minWidth: 86, sort: true
                        , templet: function (value) {
                            if (value.Medical != undefined && value.Medical.name != undefined) {
                                return value.Medical.name;
                            }
                            return "";
                        }
                    }
                    , {field: 'ILLNESS_DESC', title: '病情描述', minWidth: 101, edit: "text"}
                    , {field: "FLOW_PROGRESS", title: '流程进度', sort: true, minWidth: 86}
                    , {field: "WARD_TYPE", title: '病房类型', sort: true, minWidth: 86}
                    , {fixed: 'right', align: 'center', toolbar: '#barDemo', minWidth: 113}
                ]]
                , page: true
            });
            table.on('toolbar(test)', function (obj) {
                console.log(obj);
                let checkStatus = table.checkStatus(obj.config.id);
                console.log(checkStatus);
                switch (obj.event) {
                    //预约用户信息
                    case 'add':
                        if (checkStatus.data.length == 0) {
                            layer.msg("请选一项");
                            return;
                        }
                        layer.confirm('确定接受吗？', function (index) {
                            //向服务端发送删除指令
                            let postData = [];
                            let data = checkStatus.data;
                            for (let temp in data) {
                                postData.push(data[temp]["ID"]);
                            }
                            $.ajax({
                                url: "${pageContext.servletContext.contextPath}/reserve/acceptAppointment",
                                data: {
                                    id: postData
                                },
                                type: "POST",
                                dataType: "json",
                                success: function (data) {
                                    console.log(data);
                                    if (data.code != 0) {
                                        //提示错误
                                        layer.msg(data.msg);
                                        return;
                                    }
                                    //刷新列表
                                    tableLns.reload();
                                    layer.msg(data.msg);
                                    return;
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    // 通常 textStatus 和 errorThrown 之中
                                    // 只有一个会包含信息
                                    //this; // 调用本次AJAX请求时传递的options参数
                                }
                            });
                            layer.close(index);
                        });
                        break;
                    case 'delete':
                        let postData = [];
                        let data = checkStatus.data;
                        for (let temp in data) {
                            postData.push(data[temp]["ID"]);
                        }
                        // layer.msg('删除');
                        $.ajax({
                            url: "${pageContext.servletContext.contextPath}/reserve/cancelReservation",
                            data: {
                                id: postData
                            },
                            type: "POST",
                            dataType: "json",
                            success: function (data) {
                                console.log(data);
                                if (data.code != 0) {
                                    //错误提示
                                    layer.msg(data.msg);
                                    return;
                                }
                                //刷新表
                                tableLns.reload();
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                // 通常 textStatus 和 errorThrown 之中
                                // 只有一个会包含信息
                                //this; // 调用本次AJAX请求时传递的options参数
                            }
                        });
                        break;
                    case 'update':
                        layer.msg('编辑');
                        break;
                }
            });

            //工具条事件
            table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                let data = obj.data; //获得当前行数据
                let layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                let tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
                if (layEvent === 'takeOver') { //删除
                    layer.confirm('确定接受吗？', function (index) {
                        console.log(data);
                        // console.log(index);
                        // obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        // layer.close(index);
                        //向服务端发送删除指令
                        let id = [];
                        id.push(data.ID);
                        $.ajax({
                            url: "${pageContext.servletContext.contextPath}/reserve/acceptAppointment",
                            data: {
                                id: id
                            },
                            type: "POST",
                            dataType: "json",
                            success: function (data) {
                                console.log(data);
                                if (data.code != 0) {
                                    //提示错误
                                    layer.msg(data.msg);
                                    return;
                                }
                                //刷新列表
                                tableLns.reload();
                                layer.msg(data.msg);
                                return;
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                // 通常 textStatus 和 errorThrown 之中
                                // 只有一个会包含信息
                                //this; // 调用本次AJAX请求时传递的options参数
                            }
                        });
                        layer.close(index);
                    });
                }
                if (layEvent === "refuse") {
                    layer.confirm('确定拒绝吗？', function (index) {
                        console.log(data);
                        // console.log(index);
                        // obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        // layer.close(index);
                        //向服务端发送删除指令
                        let id = [];
                        id.push(data.ID);
                        $.ajax({
                            url: "${pageContext.servletContext.contextPath}/reserve/refuseProcessing",
                            data: {
                                id: id
                            },
                            type: "POST",
                            dataType: "json",
                            success: function (data) {
                                console.log(data);
                                if (data.code != 0) {
                                    //提示错误
                                    layer.msg(data.msg);
                                    return;
                                }
                                //刷新列表
                                tableLns.reload();
                                layer.msg(data.msg);
                                return;
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                // 通常 textStatus 和 errorThrown 之中
                                // 只有一个会包含信息
                                //this; // 调用本次AJAX请求时传递的options参数
                            }
                        });
                        layer.close(index);
                    });
                }
            });

            table.on('edit(test)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
                console.log(obj);
                console.log(obj.value); //得到修改后的值
                console.log(obj.field); //当前编辑的字段名
                console.log(obj.data); //所在行的所有相关数据
                let data = obj.data;
                let retData;
                if (obj.field == 2)
                    retData = {
                        "id": data.ID,
                        "TIME_OF_APPOINTMENT": obj.value,
                        illness_desc: null
                    };
                if (obj.field == "ILLNESS_DESC")
                    retData = {
                        "id": data.ID,
                        "TIME_OF_APPOINTMENT": null,
                        illness_desc: obj.value
                    }
                $.ajax({
                    url: "${pageContext.servletContext.contextPath}/reserve/updateTimeOfAppointment",
                    data: retData,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        layer.msg(data.msg);
                        tableLns.reload();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        // 通常 textStatus 和 errorThrown 之中
                        // 只有一个会包含信息
                        //this; // 调用本次AJAX请求时传递的options参数
                    }
                });
                //end
            });


            //$() end
        });
        <%--layui.config({--%>
        <%--    base: '/js/', //拓展模块的根目录--%>
        <%--}).extend({--%>
        <%--    dropMenu: '${pageContext.servletContext.contextPath}/static/layui/dropMenu'--%>
        <%--});--%>

        $(window).resize(function () {
            let ret = false;
            setTimeout(function () {
                ret = true;
            }, 500);
            setInterval(function () {
                if (ret == true) {
                    return;
                }
                console.log("更新了");
                tableLns.resize();
            }, 100);
        });

    });


</script>
</body>
</html>